<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class orderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $num = $request -> input('num',5);
        $keywords = $request -> input('keywords','');

        $data = DB::table('order')
                -> where('rec','like', '%'.$keywords.'%')
                -> paginate($num);
                
        return view('admin.order.index',['data' => $data, 'request' => $request -> all(), 'title' => '订单列表']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * \
     * 
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($oid)
    {
        $order = DB::table('order')->where('oid',$oid)->get();
        //商品信息 
        
        foreach ($order as $row) 
        {
            $row->oo = $row->oid;
            $row->oid = DB::table('detail')->where('oid',$row->oid)->get();
            
            foreach($row->oid as $ww)
            {
                //dd($ww->gid);
                $ww->gid = DB::table('goods')->where('gid',$ww->gid)->first();
            }
        }
         // dd($order);
      $data = DB::table('order')->where('oid',$oid)->first();
      return view('admin.order.edit',['data'=>$data,'order'=>$order]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $oid)
    {
        $data = $request -> except('_token','_method');
        $res = DB::table('order')-> where('oid',$oid) ->update($data);
        if ($res) 
        {
           return redirect('/admin/order')->with(['info'=>'修改成功']);
        }else
        {
            return back()->with(['info'=>'修改失败']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
